﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;

namespace NutriLeaf.Phone.Settings
{
    public class Settings
    {
        // Our isolated storage settings
        IsolatedStorageSettings isolatedStore;

        // The isolated storage key names of our settings
        const string PushNotificationsKeyName = "PushnotificationsKey";
        const string FbAccessTokenKeyName = "FBAccessToken";
        const string FbAccessTokenExpiresKeyName = "FBAccessTokenExpires";
        const string UserProfileKeyName = "UserProfileKey";
        const string UserKeyName = "UserKeyName";
        const string DefaultWeightUnitsKeyName = "DefaultWeightUnits";
        const string DefaultHeightUnitsKeyName = "DefaultHeightUnitsKeyName";



        // The default value of our settings
        const string UsernameSettingDefault = "";
        const string PushNotificationDefault = "";
        const string FbAccessTokenDefault = "";
        string DefualtWeightUnitsDefault = Constants.Units.Pounds;
        string DefualtHeightUnitsDefault = Constants.Units.Inch;
        DateTime FbAccessTokenExpiresDefault = DateTime.Now.AddDays(-30); //Datetime value that already expired. This is default.
        AccountService.UserProfile UserProfileDefault = null;
        AccountService.User UserDefault = null;

        /// <summary>
        /// Constructor that gets the application settings.
        /// </summary>
        public Settings()
        {
            try
            {
                // Get the settings for this application.
                isolatedStore = IsolatedStorageSettings.ApplicationSettings;
            }
            catch (Exception e)
            {

            }
        }

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (isolatedStore.Contains(Key))
            {
                // If the value has changed
                if (isolatedStore[Key] != value)
                {
                    // Store the new value
                    isolatedStore[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                isolatedStore.Add(Key, value);
                valueChanged = true;
            }

            return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="valueType"></typeparam>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
        {
            valueType value;

            // If the key exists, retrieve the value.
            if (isolatedStore.Contains(Key))
            {
                value = (valueType)isolatedStore[Key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }

            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            isolatedStore.Save();
        }

        /// <summary>
        /// Property to get and set a Push Notification Key.
        /// </summary>
        public string PushNotificationSetting
        {
            get
            {
                return GetValueOrDefault<string>(PushNotificationsKeyName, PushNotificationDefault);
            }
            set
            {
                AddOrUpdateValue(PushNotificationsKeyName, value);
                Save();
            }
        }

        /// <summary>
        /// Property to get and set Facebook AccessToken
        /// </summary>
        public string FacebookAccessToken
        {
            get
            {
                return GetValueOrDefault<string>(FbAccessTokenKeyName, FbAccessTokenDefault);
            }
            set
            {
                AddOrUpdateValue(FbAccessTokenKeyName, value);
                Save();
            }
        }


        /// <summary>
        /// Property to get and set Facebook Access Token Expires value
        /// </summary>
        public DateTime FacebookAccessTokenExpires
        {
            get
            {
                return GetValueOrDefault<DateTime>(FbAccessTokenExpiresKeyName, FbAccessTokenExpiresDefault);
            }
            set
            {
                AddOrUpdateValue(FbAccessTokenExpiresKeyName, value);
                Save();
            }
        }

        public string WeightUnits
        {
            get
            {
                return GetValueOrDefault<string>(DefaultWeightUnitsKeyName, DefualtWeightUnitsDefault);
            }
            set
            {
                AddOrUpdateValue(DefaultWeightUnitsKeyName, value);
                Save();
            }
        }


        /// <summary>
        /// Property to get and set Facebook Access Token Expires value
        /// </summary>
        public string HeightUnits
        {
            get
            {
                return GetValueOrDefault<string>(DefaultHeightUnitsKeyName, DefualtHeightUnitsDefault);
            }
            set
            {
                AddOrUpdateValue(DefaultHeightUnitsKeyName, value);
                Save();
            }
        }


        public AccountService.UserProfile UserProfile
        {
            get
            {
                return GetValueOrDefault<AccountService.UserProfile>(UserProfileKeyName, UserProfileDefault);
            }
            set
            {
                AddOrUpdateValue(UserProfileKeyName, value);
                Save();
            }
        }

        public AccountService.User User
        {
            get
            {
                return GetValueOrDefault<AccountService.User>(UserKeyName, UserDefault);
            }
            set
            {
                AddOrUpdateValue(UserKeyName, value);
                Save();
            }
        }


    }
}
